<?php
class BreadcrumbsWidget extends FWidget
{
    public $expire = 900;//15'
    private $_breadcrumbs;
    
    public function init()
    {
        //Yii::trace(__CLASS__.'.'.__FUNCTION__, 'Breadcrumbs');
        parent::init();
        if ($this->_breadcrumbs == null)
        {
            $default = property_exists(Yii::app()->controller, 'pageId') ? Yii::app()->controller->pageId : 0;
            $pageId = (int) Yii::app()->request->getParam('page_id', $default);
            //Yii::trace('$pageId '.$pageId, 'Breadcrumbs');
            
            if(($cache=Yii::app()->cache)!==null)
            {
                $key=__CLASS__.'::'.__FUNCTION__
                    .Yii::app()->request->getHostInfo()
                    .'#page_id:'.$pageId;
                if(($data=$cache->get($key))!==false)
                {
                    $this->_breadcrumbs = unserialize($data);
                    return;
                }
            }
            
            $page = Page::model()->with('defaultUrl')->findByPk($pageId);
            $this->_breadcrumbs = array();
            if (is_object($page))
            {
                if (is_object($page->defaultUrl))
                    $this->_breadcrumbs[$page->title] = Yii::app()->request->hostInfo.'/'.trim($page->defaultUrl->url, '/');
                else
                    $this->_breadcrumbs[$page->title] = '#';
                    
                $this->getParent($page);
                $this->_breadcrumbs = array_reverse($this->_breadcrumbs);

                // 'Home' does not work in other language
//                if (isset($this->_breadcrumbs['Home']))
//                    unset($this->_breadcrumbs['Home']);

                // page is not on the tree, need to add the home link
                if (count($this->_breadcrumbs) == 1) {
                    $page = Page::model()->findByAttributes(array('alias' => 'Home','site_id' => Yii::app()->cmsManager->Site['id']));
                    if ($page){
                        $this->_breadcrumbs = CMap::mergeArray(array($page->title => Yii::app()->request->hostInfo.'/index.html'), $this->_breadcrumbs);
                    }
                }

                //Yii::trace('breadcrumbs '.CVarDumper::dumpAsString($this->_breadcrumbs), 'Breadcrumbs');
            }
            
            if(isset($cache))
            {
                $sql = "SELECT last_update FROM ".SITE_TABLE_PREFIX."page_url WHERE page_id={$pageId} ORDER BY last_update DESC LIMIT 1;";
                $dependency = new CDbCacheDependency($sql);
                $cache->set($key,serialize($this->_breadcrumbs), $this->expire, $dependency);
            }
        }
    }
    
    protected function getParent($page)
    {
        $parent = $page->parent(array(
            'with'=>array('defaultUrl'),
        ));
        if (is_object($parent))
        {
            if (is_object($parent->defaultUrl))
                $this->_breadcrumbs[$parent->title] = Yii::app()->request->hostInfo.'/'.trim($parent->defaultUrl->url, '/');
            else
                $this->_breadcrumbs[$parent->title] = '#';
            $this->getParent($parent);
        }
    }
    
    public function run()
    {
        return $this->widget('zii.widgets.CBreadcrumbs', array(
            'homeLink'=> false,
            'links'=>$this->_breadcrumbs,
            'separator' => " &gt; ",
        ), true);
    }
}